import pymysql


class Mysql(object):
    def __init__(self):
        # 创建连接
        self.coon = pymysql.Connect(
            host="127.0.0.1",  # 本机地址
            port=3306,  # MySQL端口，默认3306
            user='root',  # 超级管理员用户
            password='123456',  # 数据库的密码
            db='My_data'  # 指定数据库
        )
        # 创建游标 -- 执行sql语句
        self.cursor = self.coon.cursor()

    def run(self):
        # sql插入语句
        sql = 'insert into My_data(id,nam,age,sex,love) values (1,"张三",18,"男","篮球")'
        # 执行sql语句
        self.cursor.execute(sql)
        self.coon.commit()


if __name__ == '__main__':
    My = Mysql()
    My.run()
